<?php
/**
 * This class has a function that create KML file which includes route path
 *
 * @author Ahmet
 */
class KmlGenerator {
    
    //Creates a KML file with specified query and upload it to ftp server
    public static function createPath($query, $filename, $pathColor) {
        $resp = mysql_query($query);
        if (!$resp) {
            die('Invalid query: ' . mysql_error());
        }

        // Start KML file, create parent node
        $dom = new DOMDocument('1.0', 'UTF-8');

        //Create the root KML element and append it to the Document
        $node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
        $parNode = $dom->appendChild($node);

        //Create a Folder element and append it to the KML element
        $dnode = $dom->createElement('Document');
        $documentNode = $parNode->appendChild($dnode);

        //Iterate through the MySQL results
        $array = array();

        while ($islem = mysql_fetch_array($resp, MYSQL_ASSOC)) {
            array_push($array, $islem);
        }

        $coordinates = "";

        foreach ($array as $value) {
            $coordinates .= $value['lat'] . "," . $value['lng'] . "\n";
        }
        //Create a style element for color of path
        $sNode = $dom->createElement('Style');
        $sNode->setAttribute('id', 'pathStyle');
        $styleNode = $documentNode->appendChild($sNode);
        
        //Linestyle element to specify that this style goes for path
        $lineSNode = $dom->createElement('LineStyle');
        $lineStyleNode = $styleNode->appendChild($lineSNode);
        $colorNode = $dom->createElement('color', $pathColor);
        $lineStyleNode->appendChild($colorNode);
        $widthNode = $dom->createElement('width', '4');
        $lineStyleNode->appendChild($widthNode);
        //Create a Placemark and append it to the document
        $node = $dom->createElement('Placemark');
        $placeNode = $documentNode->appendChild($node);

        //Create an id attribute and assign it the value of id column
        $placeNode->setAttribute('id', 'linestring1');

        //Create name, description, and address elements and assign them the values of 
        //the name, type, and address columns from the results

        $nameNode = $dom->createElement('name', 'My path');
        $placeNode->appendChild($nameNode);
        $descNode = $dom->createElement('description', '');
        $placeNode->appendChild($descNode);
        $styleUrlNode = $dom->createElement('styleUrl', '#pathStyle');
        $placeNode->appendChild($styleUrlNode);

        //Create a LineString element
        $lineNode = $dom->createElement('LineString');
        $placeNode->appendChild($lineNode);
        $almodenode = $dom->createElement('altitudeMode', 'relativeToGround');
        $lineNode->appendChild($almodenode);

        //Create a coordinates element and give it the value of the lng and lat columns from the results

        $coorNode = $dom->createElement('coordinates', $coordinates);
        $lineNode->appendChild($coorNode);
        $kmlOutput = $dom->saveXML();

        //Write KML file to project server
        $myFile = $filename;
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $kmlOutput);
        fclose($fh);
        
        //FTP CONNECTION ####################################
        //necessary inputs for ftp connection
        $ftpserver = "ftp.st07110364.etu.edu.tr";
        $ftpuser = "st07110364@etu.edu.tr";
        $ftppass = "3550783";
        $hedefdosya = $filename; //the file that is going to be sent
        
        //file going to be uploaded
        $kaynakdosya = $filename;

        $baglanti = ftp_connect($ftpserver);

        //making connection 

        $giris = ftp_login($baglanti, $ftpuser, $ftppass);

        //making necessary controls
        if ($baglanti && $giris) {
            //changing directory for file
            ftp_chdir($baglanti, '/public_html/');
            //uploading file to ftp server
            $yukle = ftp_put($baglanti, $kaynakdosya, $hedefdosya, FTP_BINARY);
            //controlling the upload
            if (!$yukle) {
                return "Dosya Yüklenemiyor";
            }
            //closing the ftp connection
            ftp_close($baglanti);
        } else {

            return "Ftp'ye bağlanılamadı!";
            die;
        }
        return "done";
    }
}

?>
